/**
 * 
 */
package com.zhiteng.utils.tencent.campus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**  
* <p>Title: ParamUtil.java</p>  
* <p>Description: </p>  
* @author dragon  
* @date 2018年12月16日  
* @version 1.0  
*/
public class ParamUtil {

	@SuppressWarnings({ "unchecked" })
	//	将嵌套的map转为简单的map的json字符串（如转换后key为data[type]），用于发送消息接口（get请求用）
	public static String paramMap2paramStr(Map<String, Object> paramMap) {
		String result = "";
		try {
			HashMap<String,Object> map = (HashMap<String,Object>)paramMap;
			HashMap<String, Object> cloneMap = (HashMap<String, Object>) map.clone();
			
			//	构造成data[type]为键的形式
			for (Map.Entry<String, Object> entry : map.entrySet()) {	//	不能在迭代的同时修改，因此clone
				if (entry.getValue() instanceof Map) {
					Map<String,String> innerMap = (Map<String,String>)entry.getValue();
					String outKey = entry.getKey();	// data
					for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
						String key = outKey+"["+innerEntry.getKey()+"]";
						String value = innerEntry.getValue();
						cloneMap.put(key, value);
					}
					cloneMap.remove(outKey);
				}
			}
			
			List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(cloneMap.entrySet());
			// 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
			Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
				public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
					return (o1.getKey()).toString().compareTo(o2.getKey());
				}
			});
			// 构造签名键值对的格式
			StringBuilder sb = new StringBuilder();
			for (Map.Entry<String, Object> item : infoIds) {
				if (item.getKey() != null || item.getKey() != "") {
					String key = item.getKey();
					String val = item.getValue().toString();
					if (!(val == "" || val == null)) {
						sb.append(key + "=" + val + "&");
					}
				}
			}
			
			result = sb.toString();
			//	System.out.println("result:"+result);
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked" })
	//	将嵌套的map转为简单的map（如转换后key为data[type]），用于发送消息接口（post请求用）
	public static Map<String,Object> getParamMap(Map<String, Object> paramMap) {
		HashMap<String,Object> map = (HashMap<String,Object>)paramMap;
		HashMap<String, Object> cloneMap = (HashMap<String, Object>) map.clone();//	不能在迭代的同时修改，因此clone
		try {
			//	构造成data[type]为键的形式
			for (Map.Entry<String, Object> entry : map.entrySet()) {	
				if (entry.getValue() instanceof Map) {
					Map<String,String> innerMap = (Map<String,String>)entry.getValue();
					String outKey = entry.getKey();	// data
					for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
						String key = outKey+"["+innerEntry.getKey()+"]";
						String value = innerEntry.getValue();
						cloneMap.put(key, value);
					}
					cloneMap.remove(outKey);
				}
			}
		} catch (Exception e) {
			return null;
		}
		return cloneMap;
	}
	
}
